package com.tang.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tang.common.entity.message.Message;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-21
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @RequestMapping("/sendMessage")
    public Result sendMessage(String sendUserName,String receiveUserName,String content){
        Message message = new Message();
        message.setSendUser(sendUserName);
        message.setReceiveUser(receiveUserName);
        message.setContent(content);
        boolean save = messageService.save(message);
        if(save){
            return Result.success("发送成功");
        }
        return Result.error(RecruitExceptionEnum.SEND_FAILED);
    }
    @RequestMapping("/receiveMessage")
    public Result receiveMessage(String receiveUserName){
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receive_user",receiveUserName);
        queryWrapper.eq("delete_flag",1);
        List<Message> list = messageService.list(queryWrapper);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(RecruitExceptionEnum.NO_DATA);
    }
    @RequestMapping("/deleteMessage")
    public Result deleteMessage(Integer messageId){
        Message msg = messageService.getById(messageId);
        msg.setDeleteFlag(0);
        boolean b = messageService.updateById(msg);
        if(b){
            return Result.success("删除成功");
        }
        return Result.error(RecruitExceptionEnum.DELETE_FAILED);
    }

}
